<?php

#chat.php
#
#Part of chatboss project
#
#POST the outputObject directly from the client , returns object.
#

include 'queryengine.php';
include 'functions.php';

#add actionids here
global $outputObject;
$outputObject = new protocolObject();

if(isset($_POST['inputObject']))
{
	$inputObject = json_decode((string)$_POST['inputObject']);
	$ouputObject->actionId = $inputObject->actionId;
	$outputObject->messageSender = $inputObject->messageSender;
	$outputObject->messageRecipient = $inputObject->messageRecipient;
	$outputObject->messageText = $inputObject->messageText;
	$outputObject->userPass = $inputObject->userPass;
	$outputObject->debugMode = $inputObject->debugMode;
	
	if(gettype($inputObject))
	{
		switch($inputObject->actionId)
		{
			case 1:
				sendMessage($inputObject);
				break;
			case 2:
				getMessage($inputObject);
				break;
			case 3:
				addUser($inputObject);
				break;
			case 4:
				delUser($inputObject);
				break;
			case 5:
				changePass();
				break;
			case 6:
				authPassToken();
				break;
			case 7:
				newConv();
				break;
			case 8:
				getConv();
				break;
			case 9:
				getConvText();
				break;
			case 10:
				addGroupParticipant();
				break;
			case 11:
				deleteConv();
				break;
			case 12:
				checkGroups();
				break;
			default:
				noAction($inputObject);
				break;
		}
		

		sendDebugInfo($outputObject);
		echo json_encode($outputObject);
	}
	else
	{
		$outputObject->giveExitStatus(1,"The input type is incorrect. Ensure you are using an object.");
		echo json_encode($outputObject);
	}

	
}
#error based on script input
else
{
	$outputObject->giveExitStatus(1,"This script only takes one object input");
	echo json_encode($outputObject);
}

#
#send messages to user
#
# Requires:
#
# actionId = 1
#
# messageSender
#	The userID of the user who sent the message
#
# messageRecipient
#	The userID of the message recipient
#
# messageText
#	The text of the message itself
#
function sendMessage($inputObject)
{
	#post code
	global $outputObject;
	
	$User_Send = new User();
	$User_Receive = new User();
	
	$User_Send->setUserPass($outputObject->messageSender,$outputObject->userPass);
	$User_Receive->setUserPass($outputObject->messageRecipient,null);
	
	if($User_Receive->userExists == true && $User_Send->userExists == true)
	{
		if($User_Send->passValid)
		{
			if($outputObject->messageText != "")
			{
				crowFather("insert into message(messageSender,messageRecipient,messageText,groupName) values(" . crowFather($User_Send->userName,6) . " , " . crowFather($User_Receive->userName,6) . " , " . crowFather($outputObject->messageText,6) . ",defaultGroup);",3);
				$outputObject->giveExitStatus(0,"Message sent");
			}
			else
			{
				$outputObject->giveExitStatus(1,"There is no message to send!");
			}
		}
		else
		{
			$outputObject->giveExitStatus(1,"Password was incorrect");
		}
	}
	else
	{
		$outputObject->giveExitStatus(1,"User does not exist");
	}
	
}

#
#Get a list of messages sent to a user
#
# Requires:
#
# actionId = 2
#
# messageRecipient
#	The userId of the user who received the messages.
#

function getMessage($outputObject)
{
	#get code
	global $outputObject;
	
	$authUser = new User();
	
	$authUser->setUserPass($outputObject->messageRecipient,$outputObject->userPass);
	
	if($authUser->passValid)
	{
	
		/*$messageQuery = new messageQuery();
		$messageQuery->setupQuery(array("Sender","Message"),array("Recipient",$outputObject->messageRecipient),"queryMessageView");
		$messageQuery->getResultArray();
		$messageQuery->getTable();
		$outputObject->returnData = $messageQuery->returnData;*/
		
		$outputObject->sqlQuery = "select timeStamp,messageSender,messageText from message where messageRecipient = " . crowFather($outputObject->messageRecipient,6) . " and convId = defaultGroup;";
		$sqlOutput = crowFather($outputObject->sqlQuery,4);
		$outputObject->returnData = "<table>";
		
		foreach($sqlOutput as $row)
		{
			$outputObject->returnData .= "<tr>";
			foreach( $row as $col)
			{
				$outputObject->returnData .= "<td class='tableContentCell'>" . $col . "</td>";
			}
			$outputObject->returnData .= "</td>";
		}
		$outputObject->returnData .= "</table>";
		$outputObject->giveExitStatus(0,"Table was output");
		
	}
	else
	{
		$outputObject->giveExitStatus(1,"User password was incorrect");
	}
}

#
#Add a new user
#
# Requires:
#
# actionId = 3
#
# messageRecipient
#	The user you wish to add
#
# [userPrivilegeSet]
#		Default: 1
#	The user privileges you want to add to the account. Must correspond to a valid privilege set.
#
# [userDateOfBirth]
#		Default: Today's date
#	The user's date of birth. Will default to today's date and time if you don't set it
#
# [userDescription]
#	The user's brief description for the account. Not required at all.
#
function addUser($outputObject)
{
	#add user code
	global $outputObject;
	$newUser = new User();
	$newUser->setUserPass($outputObject->messageRecipient,$outputObject->userPass);
	$newUser->addUser();
	$outputObject->giveExitStatus(0,"The user has been added");
	
}

#
#Delete an existing user
#
# Requires:
#
# actionId = 4
#
# messageSender
#	The user you wish to delete
#
function delUser($outputObject)
{
	#delete user code
	global $outputObject;
	$delUser = new User();
	$delUser->setUserPass($outputObject->messageRecipient,$outputObject->userPass);
	if($delUser->passValid)
	{
		$delUser->deleteUser();
		$outputObject->giveExitStatus(0,"The user has been added");
	}
	else
	{
		$outputObject->giveExitStatus(1,"The password for the user was incorrect");
	}
}

function changePass()
{
	global $outputObject;
	
	$changePassUser = new User();
	$changePassUser->setUserPass($outputObject->messageRecipient,$outputObject->userPass);
	
	$changePassUser->changePass();
	$outputObject->giveExitStatus(0,"The user password was changed");
}

function authPassToken()
{
	global $outputObject;
	if(isset($outputObject->userPass))
	{
		$authPassUser = new User();
		$authPassUser->setUserPass($outputObject->messageRecipient,$outputObject->userPass);
		$authPassUser->authChangePassToken($outputObject->messageText);
		$authPassUser->checkPassword();
		if($authPassUser->passValid)
		{
			$outputObject->giveExitStatus(0,"The password was change successfully");
		}
		else
		{
			$outputObject->giveExitStatus(1,"The password does not appear to have changed");
		}
	}
	else
	{
		$outputObject->giveExitStatus(1,"No new password was provided");
	}
}


#An error checking function for object/input based errors
function noAction($outputObject)
{
	#error checking function for actionId based fail
	global $outputObject;
	if($outputObject->exitCode == 2)
	{
		#Exit status unchanged (nothing ran)
		$outputObject->giveExitStatus(1,"An exit status was not provided");
	}
	
	if($outputObject->actionId > 4)
	{
		$outputObject->giveExitStatus(1,"The action type was not one of the supported actions");
	}
}

function newConv()
{
	global $outputObject;
	$convCreator = new User();
	$convCreator->setUserPass($outputObject->messageRecipient,$convCreator->userPass = $outputObject->userPass);
	$outputObject->debug = $convCreator;
	
	if($convCreator->passValid)
	{
		$newConv = new Conversation();
		$newConv->setGroup($outputObject->messageText,$convCreator);
		$newConv->newConversation();
		$outputObject->giveExitStatus($newConv->exitCode,$newConv->exitText);
	}
	else
	{
		$outputObject->giveExitStatus(1,"The password provided was incorrect");
	}
}

function getConv()
{
	global $outputObject;
	
	$conv_User = new User();
	$conv_User->setUserPass($outputObject->messageRecipient,$outputObject->userPass);
	
	$conv_Subject = new Conversation();

	if($conv_User->passValid)
	{
		
		$outputObject->returnData = $conv_Subject->getGroupOptions($conv_User);
		$outputObject->debug = $conv_Subject;
		$outputObject->giveExitStatus(0,"The group array is provided");	
		
	}
	else
	{
		$outputObject->giveExitStatus(1,"The password was incorrect");
	}
	
}

function getConvText()
{
	global $outputObject;
	$conv_User = new User();
	$conv_User->setUserPass($outputObject->messageRecipient,$outputObject->userPass);
	
	$conv_Subject = new Conversation();
	
	if($conv_User->passValid)
	{
		$conv_Subject->getListGroups($conv_User);
		$conv_Subject->messageOutput(1,$outputObject->messageText);
		$outputObject->returnData = $conv_Subject->returnData;
		$outputObject->giveExitStatus(0,"The group conversation is provided");
	}
	else
	{
		$outputObject->giveExitStatus(1,"The password was incorrect");
	}
}

function addGroupParticipant()
{
	global $outputObject;
	$conv_User = new User();
	$conv_User->setUserPass($outputObject->messageRecipient,$outputObject->userPass);
	$conv_newUser->userName = $outputObject-> messageSender;
	
	$conv_Subject = new Conversation();
	
	if($conv_User->checkPassword())
	{
		$conv_Subject->addParticipant($outputObject->messageText,$conv_User,$conv_newUser);
		$outputObject->exitCode = $conv_Subject->exitCode;
		$outputObject->exitText = $conv_Subject->exitText;
	}
	else
	{
		$outputObject->giveExitStatus(1,"The password provided was incorrect");
	}
}

function deleteConv()
{
	global $outputObject;
	$conv_Delete = new Conversation();
	
	$conv_Delete_User = new User();
	$conv_Delete_User->setUserPass($outputObject->messageRecipient,$outputObject->userPass);
	$conv_Delete->setGroup($outputObject->messageText,$conv_Delete_User);
	
	if($conv_Delete_User->passValid == true)
	{
		if($conv_Delete->isParticipant)
		{
			
		}
	}
	else
	{
		$outputObject->giveExitStatus(1,"The password provided was not accepted");
	}
	
}

function checkGroups()
{
	global $outputObject;
	$conv_Check = new Group();
	if($outputObject->messageText != "")
	{
		$conv_Check->getInfo($outputObject->messageText,6);
		$outputObject->debug = $conv_Check;
		$outputObject->giveExitStatus($conv_Check->exitCode,$conv_Check->exitText);
	}
	else
	{
		$outputObject->giveExitStatus(1,"No group name was provided");
	}
}

?>